package Tree;


import org.w3c.dom.Node;

import java.util.*;

public class BinaryTree {
    public static class BTNode {
        BTNode left;
        BTNode right;
        int value;

        BTNode(int value) {
            this.value = value;
        }
    }

    public static int count;

    private static BTNode root;

    public void createBinaryTree() {
        BTNode node1 = new BTNode(1);
        BTNode node2 = new BTNode(2);
        BTNode node3 = new BTNode(3);
        BTNode node4 = new BTNode(4);
        BTNode node5 = new BTNode(5);
        BTNode node6 = new BTNode(6);
        root = node1;
        node1.left = node2;
        node2.left = node3;
        node1.right = node4;
        node4.left = node5;
        node5.right = node6;
    }

    public void preOrder(BTNode root) {
        if (root == null) {
            return;
        }
        System.out.println(root.value);
        preOrder(root.left);
        preOrder(root.right);
    }


    public void preOrder2(BTNode root) {
        if (root == null) {
            return;
        }
        Stack<BTNode> stack = new Stack();
        BTNode cur = null;
        int count = 0;


        while (count == 0 || !stack.empty()) {
            if (stack.empty()) {
                cur = root;
            } else {
                cur = stack.pop();
            }

            System.out.println(cur.value);

            if (cur.right != null) {
                stack.push(cur.right);
            }

            if (cur.left != null) {
                stack.push(cur.left);
            }
            count++;
        }
    }

    public void infixOrder1(BTNode root) {
        if (root == null) {
            return;
        }
        infixOrder1(root.left);
        System.out.println(root.value);
        infixOrder1(root.right);
    }

    public void infixOrder2(BTNode root) {
        if (root == null) {
            return;
        }
        Stack<BTNode> stack = new Stack<>();
        BTNode cur = root;
        while (true) {
            //循环遍历向左找，直到为空
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            //2.如果为空，遍历结束
            if (stack.isEmpty()) {
                break;
            }
            //3.取栈顶元素并访问
            BTNode pop = stack.pop();
            System.out.println(pop.value);
            //4.从当前节点右子树出发
            cur = pop.right;
        }
    }

    public void postOrder1(BTNode root) {
        if (root == null) {
            return;
        }
        postOrder1(root.left);
        postOrder1(root.right);
        System.out.println(root.value);
    }

    public void postOrder2(BTNode root) {
        if (root == null) {
            return;
        }
        Stack<BTNode> stack = new Stack<>();
        BTNode cur = root;

        while (true) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }

            BTNode pop = stack.pop();

        }
    }

    /**
     * 获取树中节点的个数
     *
     * @param root 根节点
     * @return 返回树中节点的个数
     */
    public int getSize(BTNode root) {
        if (root == null) {
            return 0;
        }
        return getSize(root.left) + getSize(root.right) + 1;
    }

    /**
     * 获取节点中叶子结点的个数
     *
     * @param root 根节点
     * @return 返回叶子结点的个数
     */
    public int getLeafSize(BTNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafSize(root.left) + getLeafSize(root.right);
    }


    /**
     * 获取的k层节点的个数 规定 root所处的层级为 1
     *
     * @param root 根节点
     * @param k    层数
     * @return 返回值
     */
    public int getKSize(BTNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKSize(root.left, k - 1) + getKSize(root.right, k - 1);
    }

    /**
     * 二叉树的最大高度
     *
     * @param root 根节点
     * @return 返回值
     */
    public int getHead(BTNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(getHead(root.left) + 1, getHead(root.right) + 1);
    }

    /**
     * 二叉树的层序遍历
     *
     * @param root 根节点
     */
    public void LevelOrder(BTNode root) {
        BTNode cur = root;
        Queue<BTNode> queue = new ArrayDeque<>();

        if (cur != null) {
            queue.add(root);
            System.out.println(root.value);
        }

        while (!queue.isEmpty()) {
            cur = queue.poll();
            if (cur.left != null) {
                queue.add(cur.left);
                System.out.println(cur.left.value);
            }
            if (cur.right != null) {
                queue.add(cur.right);
                System.out.println(cur.right.value);
            }
        }
    }


    /**
     * 判断一个二叉树是否是完全二叉树
     *
     * @param root 二叉树根节点
     * @return 返回值为布尔, 代表是否是完全二叉树
     */
    public Boolean IsCompleteBinaryTree(BTNode root) {
        BTNode cur = root;
        Queue<BTNode> queue = new LinkedList<>();

        if (cur != null) {
            queue.add(root);
        }

        while (true) {
            cur = queue.poll();
            if(cur == null){
                while(!queue.isEmpty()){
                    cur = queue.poll();
                    if(cur != null){
                        return false;
                    }
                }
                return true;
            }else{
                queue.add(cur.left);
                queue.add(cur.right);
            }
        }

    }


    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.createBinaryTree();
        //binaryTree.preOrder2(root);
        //binaryTree.infixOrder2(root);
        //System.out.println(binaryTree.getSize(root));
        //System.out.println(binaryTree.getLeafSize(root));
        //System.out.println(binaryTree.getKSize(root,3)); // 2
        //System.out.println(binaryTree.getKSize(root,4)); // 1
        //System.out.println(binaryTree.getHead(root));
        binaryTree.LevelOrder(root);
        System.out.println(binaryTree.IsCompleteBinaryTree(root));

    }
}

